1.5 getAST——Syntax Tree, Generate!
In the Fasta parser, the getAST
method is used for generating abstract syntax trees.
Note: Located in pkg\front_end\lib\src\fasta\util\parser_ast.dart
.
The getAST
method receives a byte stream of Dart source code and initially creates a Utf8BytesScanner to perform the tokenize
operation. This process is similar to the first section and will not be elaborated upon again.
Subsequently, it creates a Listener and a Parser, both of which were laid out in the second section and are correspondingly applied here.
The core code is as follows:
// tokenize,返回第一个 token
Token firstToken = scanner.tokenize();
// 创建 Listener
ParserASTListener listener = new ParserASTListener();
// 创建 Parser
Parser parser = new Parser(
listener,
//...省略其它参数
);
// 触发 Parser 解析,以第一个 token 作为开始
parser.parseUnit(firstToken);
// 返回抽象语法树
return listener.data.single as CompilationUnitEnd;
In the code above, the Parser is only responsible for analyzing Token, identifying various events, and notifying the Listener.
The abstract syntax tree (AST) is actually generated in the ParserASTListener. The ParserASTListener, inheriting from Listener, implements various notification methods. In these methods, the ParserASTListener builds the AST step by step, following its own parsing rules.
Ultimately, the constructed AST is stored in the listener.data
property and returned as the output.
Let's also revisit the core logic of viewer.dart
from section four:
// Reading the source code
Uint8List bytes = new File.fromUri(uri).readAsBytesSync();
// Generate AST
ParserAstNode ast = getAST(bytes,
enableExtensionMethods: true);
// Create TUI Application
Widget widget = new QuitOnQWidget(
new WithSingleLineBottomWidget(
new BoxedWidget(
// 专门负责展示 AST 的组件
new AstWidget(ast),
),
new StatusBarWidget(),
),
);
Application app = new Application(widget);
// Run the App
app.start();
本文作者:Maeiee
本文链接:1.5 getAST——Syntax Tree, Generate!
版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!
喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!